package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.common.constant.ProductConstant;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.req.AttrGroupRelationReqVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 1、点击3级菜单，查询catelogid对应数据
     * 2、默认查询params，其中key可能是id，也可能是groupname（需要分页）
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            // 界面手动输入的条件
            //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
            attrGroupEntityQueryWrapper.and(obj ->
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key)
            );
        }
        if (0L == catelogId) {
            // 查询全部
            attrGroupEntityQueryWrapper = new QueryWrapper<>();
        } else {
            // 三级菜单
            attrGroupEntityQueryWrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                attrGroupEntityQueryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRelationAttrByGroupId(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntityList = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<Long> attrIds = relationEntityList.stream().map(item -> item.getAttrId()).collect(Collectors.toList());
        List<AttrEntity> attrEntityList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(attrIds)) {
            attrEntityList = attrService.listByIds(attrIds);
        }
        return attrEntityList;
    }

    @Override
    public void deleteRelation(AttrGroupRelationReqVo[] reqVo) {
        if (CollectionUtils.isEmpty(Arrays.asList(reqVo)))
            return;

        List<AttrAttrgroupRelationEntity> relationEntityList = Arrays.asList(reqVo).stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = AttrAttrgroupRelationEntity.builder().build();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(relationEntityList)) {
            relationService.deleteBatchRelation(relationEntityList);
        }
    }
}